package com.stu.stusystem.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.Serializable;

/**
 * @author: cxt
 * @time: 2021/1/21
 * <p>
 * 统一响应信息
 */
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "统一响应消息")
public class ApiResult<T> implements Serializable {

    public static final String SAVE = "保存成功！";
    public static final String DELETE = "删除成功！";
    public static final String UPDATE = "更新成功！";

    @ApiModelProperty("是否成功")
    private Boolean success;

    @ApiModelProperty(value = "状态码", required = true)
    private Integer code;

    @ApiModelProperty(value = "消息", required = true)
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public ApiResult(Boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public ApiResult(Integer code, String message) {
        this.success = false;
        this.code = code;
        this.message = message;
    }

    /**
     * 成功
     */
    public static <T> ApiResult<T> success() {
        return new ApiResult<>(true, 200, "成功");
    }

    /**
     * 成功：自定义code，消息
     */
    public static <T> ApiResult<T> success(int code, String message) {
        return new ApiResult<>(true, code, message);
    }

    /**
     * 成功：自定义消息
     */
    public static <T> ApiResult<T> success(String message) {
        return new ApiResult<>(true, 200, message);
    }

    /**
     * 成功：返回数据
     */
    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(true, 200, "", data);
    }

    /**
     * 成功：自定义消息、返回数据
     */
    public static <T> ApiResult<T> success(String message, T data) {
        return new ApiResult<>(true, 200, message, data);
    }

    /**
     * 成功：自定义消息、返回数据
     */
    public static <T> ApiResult<T> success(int code, String message, T data) {
        return new ApiResult<>(true, code, message, data);
    }

    /**
     * 错误
     */
    public static <T> ApiResult<T> error() {
        return new ApiResult<T>(false, 500, "");
    }

    /**
     * 错误：自定义消息
     */
    public static <T> ApiResult<T> error(ApiException e) {
        return new ApiResult<T>(false, 500, e.getMessage());
    }

    /**
     * 错误:自定义code和消息
     */
    public static <T> ApiResult<T> error(Integer code, String message) {
        return new ApiResult<T>(false, code, message);
    }
}
